﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Entity;
using DAL;
using Microsoft.VisualBasic;

namespace CafeTek
{
    public partial class FrmSelling : Form
    {
        IList<Product> _listproduct = new List<Product>();
        ProductDB _productdb = new ProductDB();
        Table Gtable = null;
        BillDB GBillDb = new BillDB();
        TableDB GtableDb = new TableDB();
        Bill Gbill = new Bill();

        public FrmSelling()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            treevproductavailable.CollapseAll();
        }

        /// <summary>
        /// Add note to treeview
        /// </summary>
        /// <param name="Node"></param>
        /// <param name="item"></param>
        public void AddToTreeView(TreeNode Node, ProductType item)
        {
            IList<ProductType> _listProductType = new List<ProductType>();
            ProductTypeDB _productTypeDb = new ProductTypeDB();

            _listProductType = _productTypeDb.GetAll(item);

            foreach (ProductType _item in _listProductType)
            {
                if (_item.Visible)
                    continue;
                // if !_item.visible,it will not display on tree view, skip this item
                TreeNode _node = new TreeNode(_item.Name);
                _node.Tag = _item.TypeId.ToString();
                Node.Nodes.Add(_node);
                AddProductToTreeView(_node, _item.TypeId);
                AddToTreeView(_node, _item);
            }
        }

        /// <summary>
        /// Add product to treeview
        /// </summary>
        /// <param name="_node"></param>
        /// <param name="TypeId"></param>
        public void AddProductToTreeView(TreeNode _node, int TypeId)
        {
            _listproduct = _productdb.GetAll(TypeId);
            foreach (Product item in _listproduct)
            {
                if (item.ProductType.TypeId == TypeId)
                {
                    TreeNode _nodeTemp = new TreeNode(item.ProductId.ToString());
                    _nodeTemp.Text = item.Name;
                    _nodeTemp.Tag = item.ProductId.ToString();
                    _nodeTemp.ForeColor = Color.BlueViolet;
                    _node.Nodes.Add(_nodeTemp);
                }
            }
        }

        /// <summary>
        /// Load product to treeview
        /// </summary>
        public void LoadTreeView()
        {
            IList<ProductType> listProductType = new List<ProductType>();
            ProductTypeDB productTypeDb = new ProductTypeDB();
            listProductType = productTypeDb.GetAll(null);

            foreach (ProductType item in listProductType)
            {
                if (!item.Visible)
                {
                    TreeNode node = new TreeNode(item.Name);
                    node.Tag = item.TypeId.ToString();
                    AddProductToTreeView(node, item.TypeId);
                    AddToTreeView(node, item);
                    treevproductavailable.Nodes.Add(node);
                }
            }
        }

        public IList<BillDetail> _listBillDetail = null;

        public BillDetailDB _BillDetailDb = new BillDetailDB();

        /// <summary>
        /// Display table extend of table original
        /// </summary>
        /// <param name="ftableExtend"></param>
        public void DisplayTableExtend(int tableExtend)
        {
            if (tableExtend == 0)
                return;

            Table table = new Table();
            table = GtableDb.GetTableFromId(tableExtend);
            lblTableExtend.Text = "This table is splited from " + table.Name;
        }

        /// <summary>
        /// Load list Bill of table if exist
        /// </summary>
        public void loadListBill()
        {
            lsvproductbill.Items.Clear();
            Gbill = new Bill();

            //get bill of table
            Gbill = _BillDetailDb.GetBillFromTableId(FrmMain.tableId);
            if (Gbill == null)
                return;

            Gtable = new Table();
            Gtable.TableId = FrmMain.tableId;

            Gbill.Table = Gtable;
            DisplayTableExtend(Gbill.TableExtent);

            _listBillDetail = new List<BillDetail>();
            _listBillDetail = _BillDetailDb.getAll(Gbill);
            foreach (BillDetail item in _listBillDetail)
            {
                ListViewItem _itemlistview = new ListViewItem(item.Product.ProductId.ToString());
                _itemlistview.SubItems.Add(item.Product.Name);
                _itemlistview.SubItems.Add(item.Quantity.ToString());
                _itemlistview.SubItems.Add(item.Price.ToString());
                lsvproductbill.Items.Add(_itemlistview);

            }
            txtTotalprice.Text = TotalPrice().ToString();
        }

        public decimal TotalPrice()
        {
            decimal _totalprice = 0;
            int _quantity = 0;
            int _price = 0;
            for (int i = 0; i < lsvproductbill.Items.Count; i++)
            {
                _quantity = int.Parse(lsvproductbill.Items[i].SubItems[2].Text);
                _price = int.Parse(lsvproductbill.Items[i].SubItems[3].Text);
                _totalprice += _quantity * _price;

            }
            return _totalprice;
        }

        private void FrmSelling_Load(object sender, EventArgs e)
        {
            this.Text = "List Bill Detail  " + FrmMain.Gtable.Name;
            loadListBill();
            LoadTreeView();
        }

        public ListViewItem CheckInlistview(int typeid)
        {
            for (int i = 0; i < lstvProductchoosen.Items.Count; i++)
            {
                if (lstvProductchoosen.Items[i].SubItems[0].Text == typeid.ToString())
                    return lstvProductchoosen.Items[i];
            }
            return null;
        }

        public void AddTolistTemp(int typeid, string Name)
        {
            ListViewItem _item;
            if ((_item = CheckInlistview(typeid)) == null)
            {
                Product _product = new Product();

                ListViewItem item = new ListViewItem(typeid.ToString());
                item.SubItems.Add(Name);
                item.SubItems.Add("1");
                lstvProductchoosen.Items.Add(item);
            }
            else
            {
                int temp = int.Parse(_item.SubItems[2].Text);
                _item.SubItems[2].Text = (temp + 1).ToString();
            }
        }

        private void treevproductavailable_DoubleClick(object sender, EventArgs e)
        {
            if (treevproductavailable.SelectedNode.Nodes.Count == 0 && treevproductavailable.SelectedNode.ForeColor == Color.BlueViolet)
            {
                int TypeId = int.Parse(treevproductavailable.SelectedNode.Tag.ToString());
                string ProductName = treevproductavailable.SelectedNode.Text;
                AddTolistTemp(TypeId, ProductName);
            }
        }

        /// <summary>
        /// Add product list to Bill
        /// </summary>
        public void AddListProductToBill()
        {
            for (int i = 0; i < lstvProductchoosen.Items.Count; i++)
            {
                Product _productTemp = new Product();
                _productTemp.ProductId = int.Parse(lstvProductchoosen.Items[i].SubItems[0].Text);

                Product productById = new Product();
                productById = _productdb.GetProductById(_productTemp.ProductId);
                int quantity = int.Parse(lstvProductchoosen.Items[i].SubItems[2].Text);

                if (_BillDetailDb.checkBillDetail(Gbill, _productTemp, _listBillDetail) == false)
                {
                    try
                    {
                        _BillDetailDb.AddProductToBIll(_productTemp, Gbill, quantity, productById.Price);
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                }
                else
                {
                    quantity += _BillDetailDb.GetQuantityFromList(_productTemp.ProductId, _listBillDetail);
                    _BillDetailDb.UpdateProductToBill(_productTemp, Gbill, quantity);
                }
            }
            lstvProductchoosen.Items.Clear();
            loadListBill();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (lstvProductchoosen.Items.Count == 0)
            {
                MessageBox.Show("don't product", "Error");
                return;
            }
            if (Gbill == null)
            {
                Gbill = GBillDb.AddNewBill(frmLogin._emp, FrmMain.tableId);
            }
            if (Gbill.Status == BillStatus.Payed)
            {
                MessageBox.Show("You need close table");
                return;
            }
            AddListProductToBill();
        }

        private void btnRemove_Click(object sender, EventArgs e)
        {
            if (lstvProductchoosen.SelectedItems.Count == 0)
                return;

            lstvProductchoosen.Items.Remove(lstvProductchoosen.SelectedItems[0]);
        }

        private void btnAddQuantity_Click(object sender, EventArgs e)
        {
            if (lstvProductchoosen.SelectedItems.Count == 0)
            {
                MessageBox.Show("Please choose One item");
                return;
            }
            string quantity = Interaction.InputBox("Input the Quantity", "Input Data", "0", 200, 200);
            int _quantity;
            if (int.TryParse(quantity, out  _quantity) == false)
            {
                MessageBox.Show("Please input correct number format", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            lstvProductchoosen.SelectedItems[0].SubItems[2].Text = _quantity.ToString();
        }

        private void lstvProductchoosen_DoubleClick(object sender, EventArgs e)
        {
            if (lstvProductchoosen.SelectedItems.Count == 0)
            {
                MessageBox.Show("Please choose One item");
                return;
            }
            string quantity = Interaction.InputBox("Input the Quantity", "Input Data", "0", 200, 200);
            int _quantity;
            if (int.TryParse(quantity, out  _quantity) == false)
            {
                MessageBox.Show("Please input correct number format", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            if (_quantity == 0)
                return;
            lstvProductchoosen.SelectedItems[0].SubItems[2].Text = _quantity.ToString();
        }

        private void btnPayment_Click(object sender, EventArgs e)
        {
            if (Gbill == null)
            {
                MessageBox.Show("The bill is not create");
                return;
            }
            if (Gbill.Status == BillStatus.Payed)
                MessageBox.Show("This tables is payed, Dont't need pay more");
            else
            {
                FrmPrintPay frm = new FrmPrintPay();
                frm.printPay(_BillDetailDb.GetDataviewPrint(Gbill));
                frm.Show();
                _BillDetailDb.PayTheBill(Gbill);
            }
            this.Close();
        }

        private void btncancel_Click(object sender, EventArgs e)
        {
            lstvProductchoosen.Items.Clear();
        }

        private void btnclose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void splitContainer1_Panel2_Paint(object sender, PaintEventArgs e)
        {

        }
    }
}
